import React, {createContext, useReducer} from "react";
import reducer from "./reducer";

interface stateTypeItem {
    id: number;
    title: string,
    icon: string,
    url: string,
    parent: number|null,
    desc: string,
    sorts: number,
    conditions: number,
}

interface stateType<T> {
    breadArr: Array<T>
}

const breadArr: stateTypeItem[] = [
    {
        id: 1,
        title: "首页",
        icon: "HomeOutlined",
        url: "/home",
        parent: null,
        desc: "首页",
        sorts: 0,
        conditions: 1,
    }
];

const initState: stateType<stateTypeItem> = {
    breadArr: breadArr
};


export const BreadContext  = createContext({breadArr:initState});

function BreadStore(props: any){
    const [state, dispatch] = useReducer(reducer, initState);
    return (
        <BreadContext.Provider value={{state, dispatch}}>
            {props.children}
        </BreadContext.Provider>
    );
}

// 自定义state的hooks
export const useBreadStore = () => React.useContext(BreadContext);

export default BreadStore;